﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace ShoppingList.Helpers
{
    public static class TimeHelpers
    {
     public static   string[] months = {"En.", "Feb.", "Mar.", "Abr.", 

                "May.", "Jun.", "Jul.", 

                "Agos.", "Sept.", "Oct.", "Nov.", "Dic."}; 

        #region Weeks
        public static DateTime GetStartOfLastWeek()
        {
            int DaysToSubtract = (int)DateTime.Now.DayOfWeek + 7;
            DateTime dt = DateTime.Now.Subtract(TimeSpan.FromDays(DaysToSubtract));
            return new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0, 0);
        }

        public static DateTime GetEndOfLastWeek()
        {
            DateTime dt = GetStartOfLastWeek().AddDays(6);
            return new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59, 999);
        }

        public static DateTime GetStartOfCurrentWeek()
        {
            DateTime dt = DateTime.Now;
            int diff = dt.DayOfWeek - DayOfWeek.Monday;
            if (diff < 0)
            {
                diff += 7;
            }

            return dt.AddDays(-1 * diff).Date; 

            //int DaysToSubtract = (int)DateTime.Now.DayOfWeek;
            //DateTime dt = DateTime.Now.Subtract(TimeSpan.FromDays(DaysToSubtract));
            //return new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0, 0);
        }

        public static DateTime GetEndOfCurrentWeek()
        {
            DateTime dt = GetStartOfCurrentWeek().AddDays(6);
            return new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59, 999);
        }
        #endregion 
        #region Months

        /// <summary> 

        /// Get the first day of the month for 

        /// any full date submitted 

        /// </summary> 

        /// <param name="dtDate"></param> 

        /// <returns></returns> 

        public static DateTime GetFirstDayOfMonth(DateTime dtDate)
        {

            // set return value to the first day of the month 

            // for any date passed in to the method 



            // create a datetime variable set to the passed in date 

            DateTime dtFrom = dtDate;



            // remove all of the days in the month 

            // except the first day and set the 

            // variable to hold that date 

            dtFrom = dtFrom.AddDays(-(dtFrom.Day - 1));



            // return the first day of the month 

            return dtFrom;

        }



        /// <summary> 

        /// Get the first day of the month for a 

        /// month passed by it's integer value 

        /// </summary> 

        /// <param name="iMonth"></param> 

        /// <returns></returns> 

        public static DateTime GetFirstDayOfMonth(int iMonth)
        {

            // set return value to the last day of the month 

            // for any date passed in to the method 



            // create a datetime variable set to the passed in date 

            DateTime dtFrom = new DateTime(DateTime.Now.Year, iMonth, 1);



            // remove all of the days in the month 

            // except the first day and set the 

            // variable to hold that date 

            dtFrom = dtFrom.AddDays(-(dtFrom.Day - 1));



            // return the first day of the month 

            return dtFrom;

        }



        /// <summary> 

        /// Get the last day of the month for any 

        /// full date 

        /// </summary> 

        /// <param name="dtDate"></param> 

        /// <returns></returns> 

        public static DateTime GetLastDayOfMonth(DateTime dtDate)
        {

            // set return value to the last day of the month 

            // for any date passed in to the method 



            // create a datetime variable set to the passed in date 

            DateTime dtTo = dtDate;



            // overshoot the date by a month 

            dtTo = dtTo.AddMonths(1);



            // remove all of the days in the next month 

            // to get bumped down to the last day of the 

            // previous month 

            dtTo = dtTo.AddDays(-(dtTo.Day));



            // return the last day of the month 

            return dtTo;

        }



        /// <summary> 

        /// Get the last day of a month expressed by it's 

        /// integer value 

        /// </summary> 

        /// <param name="iMonth"></param> 

        /// <returns></returns> 

        public static DateTime GetLastDayOfMonth(int iMonth)
        {



            // set return value to the last day of the month 

            // for any date passed in to the method 



            // create a datetime variable set to the passed in date 

            DateTime dtTo = new DateTime(DateTime.Now.Year, iMonth, 1);



            // overshoot the date by a month 

            dtTo = dtTo.AddMonths(1);



            // remove all of the days in the next month 

            // to get bumped down to the last day of the 

            // previous month 

            dtTo = dtTo.AddDays(-(dtTo.Day));



            // return the last day of the month 

            return dtTo;



        }

        public static IEnumerable<DateTime> GetWeeks(DateTime date, DayOfWeek startDay)
        {
            var list = new List<DateTime>();
            DateTime first = new DateTime(date.Year, date.Month, 1);

            for (var i = first; i < first.AddMonths(1); i = i.AddDays(1))
            {
                if (i.DayOfWeek == startDay)
                    list.Add(i);
            }

            return list;
        } 


        #endregion

    }
}
